; (function () {
  var wHeight = getViewportSize().height,
    sHeight = getScrollSize().height,
    playing = false,
    t = null;


  var AutoReader = function (opt) {
    this.playBtn = opt.playBtn;
    this.sTopBtn = opt.sTopBtn;
    this.playImg = opt.playImg;
    this.pauseImg = opt.pauseImg;

    if (!this.playBtn || !this.sTopBtn || !this.playImg || !this.pauseImg) {
      console.log("对不起，4个配置项必须全部配置");
      return;
    }

    var _self = this;

    addEvent(this.sTopBtn, 'click', function () {
      window.scrollTo(0, 0);
      clearInterval(t);
      _self.playBtn.style.backgroundImage = 'url(' + _self.playImg + ')';
      playing = false
    })

    addEvent(window, 'scroll', function () {
      _self.sTopBtnShow.call(_self);
    })

    addEvent(this.playBtn, 'click', function () {
      _self.setAutoPlay.call(_self);
    })
  }

  AutoReader.prototype = {
    setAutoPlay: function () {
      var sTop = getScrollOffset().top,
        _self = this;

      if (sHeight === wHeight + sTop) {
        return;
      }

      if (!playing) {
        t = setInterval(function () {
          var sTop = getScrollOffset().top;

          if (sHeight <= wHeight + sTop) {
            clearInterval(t);
            _self.playBtn.style.backgroundImage = 'url(' + _self.playImg + ')';
            playing = false;
            return;
          } else {
            window.scrollBy(0, 1);
            _self.playBtn.style.backgroundImage = 'url(' + _self.pauseImg + ')';
          }
        }, 1);
        playing = true;
      } else {
        clearInterval(t);
        _self.playBtn.style.backgroundImage = 'url(' + _self.playImg + ')';
        playing = false;
      }
    },
    sTopBtnShow: function () {
      var sTop = getScrollOffset().top,
        sTopBtn = this.sTopBtn;
      sTopBtn.style.display = sTop ? 'block' : 'none';
    }
  }

  window.AutoReader = AutoReader
})();